<?php
/**
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.niushop.com
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 */

namespace app\admin\controller;

use app\model\shop\Shop as ShopModel;
use app\model\system\Promotion as PrmotionModel;
use app\model\system\Site as SiteModel;
use phpoffice\phpexcel\Classes\PHPExcel;
use phpoffice\phpexcel\Classes\PHPExcel\Writer\Excel2007;
use think\facade\Cache;

/**
 * 商家管理 控制器
 * 版本 1.0.6
 */
class Shop extends BaseAdmin
{
	
	/******************************* 商家列表及相关操作 ***************************/
	
	/**
	 * 商家列表
	 */
	public function lists()
	{
		if (request()->isAjax()) {
			$page = input('page', 1);
			$page_size = input('page_size', PAGE_LIST_ROWS);
			$search_text = input('search_text', '');
			$username = input('username', '');
			
			$condition = [];
			if ($search_text) {
				$condition[] = [ 'site_name', 'like', '%' . $search_text . '%' ];
			}
			if ($username) {
				$condition[] = [ 'username', 'like', '%' . $username . '%' ];
			}
			
			$order = 'site_id desc';
			$field = '*';
			
			$site_model = new SiteModel();
			return $site_model->getSitePageList($condition, $page, $page_size, $order, $field);
			
		} else {
			
			$cert = Cache::get("niushop_cert");
			$type = $cert['type'];
			$this->assign("business_type", $type);
			return $this->fetch('shop/lists');
		}
	}
	
	/**
	 * 商家详情
	 */
	public function shopDetail()
	{
		$site_id = input('site_id', 0);
		$site_id = $site_id ? : request()->siteid();
		
		$site_model = new SiteModel();
		$site_info = $site_model->getSiteInfo([ [ 'site_id', '=', $site_id ] ], '*');
		
		$addon_array = explode(',', $site_info['data']['site_addon']);
		
		$promotion_model = new PrmotionModel();
		$promotions = $promotion_model->getPromotions();
		$promotions = $promotions['shop'];
		foreach ($promotions as $key => &$promotion) {
			if (!empty($promotion['is_developing'])) {
				unset($promotions[ $key ]);
				continue;
			}
			$promotion['is_checked'] = 0;
			if (in_array($promotion['name'], $addon_array)) {
				$promotion['is_checked'] = 1;
			} else {
				unset($promotions[ $key ]);
			}
		}
		
		$this->assign('promotions', $promotions);
		$this->assign('shop_info', $site_info['data']);
		
		return $this->fetch('shop/shop_detail');
	}
	
	/**
	 * 商家添加
	 */
	public function addShop()
	{
		$promotion_model = new PrmotionModel();
		$promotions = $promotion_model->getPromotions();
		if (request()->isAjax()) {
			//店铺信息
			$shop_data = [
				'site_name' => input('site_name', ''),//店铺名称
				'site_domain' => input('site_domain', ''),
				'site_addon' => input('addons', ''),
				'expire_time' => 0,
				'sys_uid' => $this->sys_uid
			];
			
			$shop_model = new SiteModel();
			$this->addLog("添加商家:" . $shop_data['site_name']);
			return $shop_model->addSite($shop_data);
			
		} else {
			//获取可应用的模块
			foreach ($promotions['shop'] as $key => $promotion) {
				if (!empty($promotion['is_developing'])) {
					unset($promotions['shop'][ $key ]);
					continue;
				}
			}
			$this->assign("promotions", $promotions['shop']);
			return $this->fetch('shop/add_shop');
		}
	}
	
	/**
	 *  删除店铺
	 */
	public function delete()
	{
		$site_id = input('site_id', 0);
		$site_id = $site_id ? : request()->siteid();
		
		$shop_model = new ShopModel();
		return $shop_model->deleteShop($site_id);
	}
}